package com.wswy.wzcx.ui.main.community

import com.che.libcommon.api.BaseResult
import com.che.libcommon.api.SimpleResultObserver
import com.che.libcommon.utils.RxBus
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.model.topic.TopicModel
import com.wswy.wzcx.module.InternalMsgCode
import com.wswy.wzcx.statistics.StatTools
import com.wswy.wzcx.statistics.StatisticsId


class TopicVO(val type:Int,
                   val model:TopicModel,
                   val date:CharSequence?=null,
                   var like:Int=0,
                   var reply:Int = 0,
                   var liked: Boolean = false,
              val contentPreview:CharSequence?=null,
              val imageSuffix:String?=null,
              val fromMainList:Boolean = false):InteractiveVO{

    override fun addLike(send: Boolean) {

        if (!liked && send){
            Api.get().topicAddLike(model.id).subscribe(object : SimpleResultObserver(){
                override fun onSuccess() {
                    if (fromMainList){
                        StatTools.sendClick(AppContext.getContext(),StatisticsId.bbs_list_like_done)
                    }else{
                        StatTools.sendClick(AppContext.getContext(),StatisticsId.bbs_detail_like)
                    }
                    RxBus.getDefault().postWithCode(InternalMsgCode.CODE_TOPIC_LIKE,model)
                }

                override fun onApiError(result: BaseResult<*>?) {
                }

            })
        }
        like++
        liked = true
    }

    override fun removeLike() {
        like--
        liked = false
    }


    override fun isLiked(): Boolean {
        return liked
    }

    override fun getLikeCount() = like

    override fun getReviewCount() = reply

    fun addReviewCount(){
        reply++
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as TopicVO

        if (type != other.type) return false
        if (model != other.model) return false
        if (date != other.date) return false
        if (like != other.like) return false
        if (reply != other.reply) return false
        if (liked != other.liked) return false

        return true
    }

    override fun hashCode(): Int {
        var result = type
        result = 31 * result + model.hashCode()
        result = 31 * result + (date?.hashCode() ?: 0)
        result = 31 * result + like
        result = 31 * result + reply
        result = 31 * result + liked.hashCode()
        return result
    }


}